package com.szh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.szh.domain.PaymentInfo;
import com.szh.enums.AliPayTradeState;
import com.szh.enums.PayType;
import com.szh.mapper.PaymentInfoMapper;
import com.szh.service.PaymentInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentInfoServiceImpl.class);

    /**
     * 记录支付日志，微信
     */
    @Override
    public void createPaymentInfo(String plainText) {
        LOGGER.info("微信，记录支付日志");
        Gson gson = new Gson();
        HashMap plainTextMap = gson.fromJson(plainText, HashMap.class);
        //订单号
        String orderNo = (String) plainTextMap.get("out_trade_no");
        //业务编号
        String transactionId = (String) plainTextMap.get("transaction_id");
        //支付类型
        String tradeType = (String) plainTextMap.get("trade_type");
        //交易状态
        String tradeState = (String) plainTextMap.get("trade_state");
        //用户实际支付金额
        Map<String, Object> amount = (Map<String, Object>) plainTextMap.get("amount");
        Integer payerTotal = ((Double) amount.get("payer_total")).intValue();
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setTradeType(tradeType);
        paymentInfo.setTradeState(tradeState);
        paymentInfo.setPayerTotal(payerTotal);
        this.save(paymentInfo);
    }

    /**
     * 记录支付日志，支付宝
     * @param params 通知参数
     */
    @Override
    public void createPaymentInfo(Map<String, String> params) {
        LOGGER.info("支付宝，记录支付日志");
        String orderNo = params.get("out_trade_no");
        String transactionId = params.get("trade_no");
        String tradeStatus = params.get("trade_status");
        String totalAmount = params.get("total_amount");
        int totalAmountValue = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentType(PayType.ALIPAY.getType());
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setTradeType("电脑网站支付");
        paymentInfo.setTradeState(tradeStatus);
        paymentInfo.setPayerTotal(totalAmountValue);
        Gson gson = new Gson();
        String content = gson.toJson(params, Map.class);
        paymentInfo.setContent(content);
        this.save(paymentInfo);
    }

    /**
     * 根据订单号更新支付记录的状态为已关闭
     * @param orderNo 订单号
     * @param aliPayTradeState 已关闭状态
     */
    @Override
    public void updateStatusByOrderNo(String orderNo, AliPayTradeState aliPayTradeState) {
        LOGGER.info("更新支付记录 ===> {}，状态更新为 ===> {}", orderNo, aliPayTradeState.getType());
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeState(aliPayTradeState.getType());
        paymentInfo.setUpdateTime(new Date());
        this.update(paymentInfo, queryWrapper);
    }
}
